package com.codegero.android.client.sms.gateway.boundary;

import java.util.List;

import com.codegero.android.client.sms.gateway.R;
import com.codegero.android.client.sms.gateway.entity.Response;
import com.codegero.android.client.sms.gateway.stored.DatabaseManager;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DaftarResponse extends ListActivity implements OnClickListener{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.general_list);	
		TextView title = (TextView) findViewById(R.id.title);
		title.setText("Daftar Response");
		databaseManager = new DatabaseManager(this);
		responses = databaseManager.getAllResponses();
		mAdapter = new AdapterDaftarResponse
				(this, R.layout.general_item, responses);
		getListView().setAdapter(mAdapter);
		ImageView button = (ImageView) findViewById(R.id.buttonGeneral);
		button.setOnClickListener(this);
	}
	
	private void refresh(){
		responses = databaseManager.getAllResponses();
		mAdapter.notifyDataSetChanged();
	}
	
	private class AdapterDaftarResponse extends ArrayAdapter<Response>{

		public AdapterDaftarResponse(Context context,
				int textViewResourceId, List<Response> objects) {
			super(context, textViewResourceId, objects);
			this.context 	= context;
			this.resource 	= textViewResourceId;
			responses = objects;
		}
		

		@Override
		public int getCount() {
			return responses!=null?responses.size():0;
		}
		

		@Override
		public Response getItem(int position) {
			return responses.get(position);
		}


		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			final Response response = getItem(position);
			
			if(response == null) return null;
			
			LayoutInflater inflater = (LayoutInflater) context
			        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			View rowView = inflater.inflate(resource, parent, false);
			
			TextView id 		= (TextView) rowView.findViewById(R.id.generalId);
			TextView message	= (TextView) rowView.findViewById(R.id.generalMessage);
			
			id.setText(response.getId());
			message.setText(response.getMessage());
			LinearLayout llBackground = (LinearLayout) rowView.findViewById(R.id.background);
			if(position%2==0){
				llBackground.setBackgroundColor(Color.rgb(0xCC, 0xFF, 0xFF));
			}else{
				llBackground.setBackgroundColor(Color.WHITE);
			}
			
			rowView.setOnLongClickListener(new View.OnLongClickListener() {
				
				public boolean onLongClick(View arg0) {
					Bundle bundle = new Bundle();
					bundle.putString("id", response.getId());
					bundle.putString("message", response.getMessage());
					showDialog(RESPONSE_CHOOSE, bundle);
					return true;
				}
			});
			return rowView;
		}
		
		private final Context context;
		private final int resource;
		private List<Response> responses;
	}
	
	@Override
	protected Dialog onCreateDialog(int id, final Bundle bundle) {
		switch(id){
			case RESPONSE_ADD:{
				final Dialog dialog = new Dialog(this);
				dialog.setContentView(R.layout.response_add);
				final TextView tvKode = (TextView) dialog.findViewById(R.id.responseTxtKode);
				final TextView tvMessage = (TextView) dialog.findViewById(R.id.responseTxtMessage);
				if(bundle!=null){
					tvKode.setText(bundle.getString("id"));
					tvMessage.setText(bundle.getString("message"));
				}
				Button buttonOk		= (Button) dialog.findViewById(R.id.responseBtnOk);
				buttonOk.setOnClickListener(new View.OnClickListener() {
					
					public void onClick(View v) {
						Response response = new Response();
						response.setId(tvKode.getText().toString());
						response.setMessage(tvMessage.getText().toString());
						if(bundle!=null){
							databaseManager.updateResponse(response);
						}else{
							databaseManager.addResponse(response);
						}

						refresh();
						dialog.dismiss();
					}
				});
				Button buttonCancel	= (Button) dialog.findViewById(R.id.responseBtnCancel);
				buttonCancel.setOnClickListener(new View.OnClickListener() {
					
					public void onClick(View v) {
						dialog.dismiss();
					}
				});
				return dialog;
			}case RESPONSE_CHOOSE:{
				final Dialog dialog = new Dialog(this);
				dialog.setContentView(R.layout.choose_general);
				
				Button btnDetail = (Button) dialog.findViewById(R.id.chooseBtnDetail);
				btnDetail.setVisibility(View.INVISIBLE);
				
				Button btnEdit = (Button) dialog.findViewById(R.id.chooseBtnEdit);
				btnEdit.setOnClickListener(new View.OnClickListener() {
					public void onClick(View v) {
						showDialog(RESPONSE_ADD, bundle);
						dialog.dismiss();
					}
				});
				
				Button btnDelete = (Button) dialog.findViewById(R.id.chooseBtnDelete);
				btnDelete.setOnClickListener(new View.OnClickListener() {
					
					public void onClick(View v) {
						Response response = new Response();
						response.setId(bundle.getString("id"));
						response.setMessage(bundle.getString("message"));
						databaseManager.deleteResponse(response);

						refresh();
						dialog.dismiss();
					}
				});
				return dialog;
			}
		}
		return super.onCreateDialog(id);
	}
	
	public void onClick(View v) {
		showDialog(RESPONSE_ADD);
	}

	private ArrayAdapter<Response> mAdapter;
	private List<Response> responses;
	private DatabaseManager databaseManager;
	
	private static final int RESPONSE_ADD 	= 0;
	private static final int RESPONSE_CHOOSE= 1;
	
}
